/**
 * @file tiny_fs.h
 * @author 张恒宣
 * @LastEditTime 2025/5/09
 *
 * MIT License
 *
 * Copyright (c) 2025 张恒宣 luckkeymail@163.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef __TINY_FS_H__
#define __TINY_FS_H__

#include <stdio.h>
#include "tiny_def.h"
#include <string.h>
#include "tiny_malloc.h"


#define TINY_FS_MAX_PATH 128

typedef enum
{
    TINY_FS_STYLE_FILE = 0,
    TINY_FS_STYLE_DEV,
    TINY_FS_STYLE_DIR,
} tiny_fs_style_t;

static inline const char* tiny_fs_style_to_str(tiny_fs_style_t style)
{
    switch (style)
    {
        case TINY_FS_STYLE_FILE:
            return "file";
        case TINY_FS_STYLE_DEV:
            return "dev";
        case TINY_FS_STYLE_DIR:
            return "dir";
        default:
            return "unknown";
    }
}

typedef struct
{
    int (*open)(const char *pathname, int flags, tiny_uint32_t mode);
    int (*close)(const char *pathname);
    size_t (*write) (const char *pathname, void * buf, size_t count); 
    size_t (*read)(const char *pathname, void *buf, size_t count);
    int (*remove)(const char *path);
    int (*rename)(const char *oldpath, const char *newpath);
    void (*scan_dir)(const char *path, int (*callback)(const char *name, tiny_fs_style_t style));
} tiny_fs_operator_t;

typedef struct __tiny_fs
{
    const char *name;
    tiny_fs_style_t style;
    tiny_fs_operator_t *op;
    struct __tiny_fs *next;
} tiny_fs_t;

typedef struct __tiny_fs_info
{
    int fd;
    char path[TINY_FS_MAX_PATH];
    tiny_fs_operator_t *op;
    struct __tiny_fs_info *next;
    struct __tiny_fs_info *prev;
} tiny_fs_info_t;

int tiny_mount(const char *name, tiny_fs_operator_t *op);
int tiny_open(const char *path, int flags, tiny_uint32_t mode);
int tiny_close(int fd);
size_t tiny_write(int fd, void *buf, size_t count);
size_t tiny_read(int fd, void *buf, size_t count);
int tiny_remove(const char *path);
int tiny_rename(const char *oldpath, const char *newpath);
void tiny_scan_dir(const char *path, int (*callback)(const char *name, tiny_fs_style_t style));


#endif //!__TINY_FS_H__
